'use strict';
/**
 * Created by FreezeSoul on 2015/9/20.
 */
define([
        'angular',
        'adConfig',
        'app/modules/login/define',
        'app/modules/login/services/login'
    ],
    function (angular, adConfig) {
        angular.module('ad.login')
            .controller('adLoginController', ['$rootScope', '$scope', '_', '$state', '$location', 'Notification', 'adLoginService',
                function ($rootScope, $scope, _, $state, $location, Notification, service) {
                    $scope.username = "";
                    $scope.password = "";
                    $scope.appVersion = adConfig.appVersion;
                    $scope.submit = function () {
                        service.login($scope.username, $scope.password).then(function (data) {
                            var obj = angular.fromJson(data);
                            if (obj.result == 'success') {
                                $rootScope.currentUser = {
                                    UserId: obj.data.user.user_id,
                                    UserName: obj.data.user.user_name,
                                    UserAlias: obj.data.user.user_alias,
                                    UserToken: obj.data.token,
                                    UserRoles: [],
                                    IsConfigEditor: false
                                };
                                _.each(obj.data.roles, function (role) {
                                    $rootScope.currentUser.UserRoles.push({
                                        RoleId: role.role_id,
                                        RoleName: role.role_name,
                                        RoleType: role.role_type
                                    });
                                });

                                Notification.success('登录成功！');

                                if (_.find($rootScope.currentUser.UserRoles, function (role) {
                                    return role.RoleType == 0;
                                })) {
                                    $rootScope.currentUser.IsConfigEditor = true;
                                    window.sessionStorage ? window.sessionStorage.setItem('amazing-dashboard-user', angular.toJson($rootScope.currentUser)) : null;
                                    $state.go('editor.home');
                                } else {
                                    $rootScope.currentUser.IsConfigEditor = false;
                                    window.sessionStorage ? window.sessionStorage.setItem('amazing-dashboard-user', angular.toJson($rootScope.currentUser)) : null;
                                    $state.go('player');
                                }

                            } else {
                                Notification.error('登录失败！');
                            }
                        }, function (data) {
                            Notification.error('服务异常！');
                        });
                        return false;
                    }
                }]);
    });